package com.gitee.qdbp.jdbc.stream;

import com.gitee.qdbp.able.jdbc.condition.DbWhere;
import com.gitee.qdbp.able.jdbc.fields.Fields;
import com.gitee.qdbp.able.jdbc.ordering.Orderings;
import com.gitee.qdbp.jdbc.api.CrudDao;

/**
 * Select+Where后续操作<br>
 * 后续操作有: find / orderBy / paging / list
 *
 * @author zhaohuihua
 * @version 20210529
 */
public class CrudOnSelectWhereByStream<T> extends CrudOnSelectOrderByStream<T> {

    CrudOnSelectWhereByStream(CrudDao<T> dao, Fields fields, DbWhere where) {
        super(dao, fields, where, Orderings.NONE);
    }

    /**
     * 根据查询条件获取对象<br>
     * <pre>
    SysUser user = qdbcBoot.crudStream(SysUser.class)
        .select("id,userCode,realName") // 只查某些字段
        // .selectExclude("password") // 排除掉密码字段
        .where("id").equals("U00001")
            .and("userState").in(UserState.NORMAL, UserState.LOCKED)
            .end()
        .find();
     * </pre>
     * 
     * @return 实体对象
     */
    public T find() {
        return dao.find(fields, where);
    }

    /**
     * 设置OrderBy条件<br>
     * <pre>
    List&lt;SysUser&gt; users = qdbcBoot.crudStream(SysUser.class)
        .select("id,userCode,realName") // 只查某些字段
        // .selectExclude("password") // 排除掉密码字段
        .whereBy((where) -> {
            ...
        })
        .orderBy("createTime desc")
        .list();
     * </pre>
     * 
     * @param orderings OrderBy条件
     * @return 返回后续流式操作对象
     */
    public CrudOnSelectOrderByStream<T> orderBy(String orderings) {
        return orderBy(Orderings.of(orderings));
    }

    /**
     * 设置OrderBy条件<br>
     * <pre>
    List&lt;SysUser&gt; users = qdbcBoot.crudStream(SysUser.class)
        .select("id,userCode,realName") // 只查某些字段
        // .selectExclude("password") // 排除掉密码字段
        .whereBy((where) -> {
            ...
        })
        .orderBy("createTime desc")
        .list();
     * </pre>
     * 
     * @param orderings OrderBy条件
     * @return 返回后续流式操作对象
     */
    public CrudOnSelectOrderByStream<T> orderBy(Orderings orderings) {
        return new CrudOnSelectOrderByStream<>(dao, fields, where, orderings);
    }
}
